<?php

class HmUser_HmUserController extends Fw_Controller_Action
{
	public function init()
	{
		parent::init();
		$this->setTemplatePath('hm-user');
	}

	public function listAction()
	{
		$this->initSession();
		$model = $this->_getModel('HmUser');
		$list_user = $this->_model;
		$rowset = $list_user->fetchAll();

		$rowCount = count($rowset);
		$this->view->list_user = $rowset;
		$this->view->rowCount  = $rowCount;
	}
	
	public function addAction()
	{
		$this->initSession();
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost('add');
			if ($formData) {
				
				$model = $this->_getModel('HmUser');
				$add   = $this->_model;
				if(!$this->_request->getPost('phone')) { $phone = NULL; } else { $phone = $this->_request->getPost('phone'); }
				$row                = $add->createRow();
				$row->name          = $this->_request->getPost('name');
				$row->email         = $this->_request->getPost('email');
				$row->passwd        = $this->_request->getPost('passwd');
				$row->category      = $this->_request->getPost('category');
				$row->phone         = $phone;
				// $row->group_id      = $this->_request->getPost('group_id');
				$row->date_register = date('Y-m-d H:i:s');

				$row->save();
				
				return $this->_helper->redirector('list');
			}
		}
		/*require_once APPLICATION_PATH . '/models/HmGroups.php';
		$table = new Model_HmGroups();
		$view_list = $table->fetchAll();
		$rowCount = count($view_list);
		$this->view->view_list = $view_list;
		$this->view->rowCount = $rowCount;*/
	}
	
	public function editAction()
	{
		$this->initSession();
		$user_id = (int)$this->_request->getParam('user_id');
		$model   = $this->_getModel('HmUser');
		$edit    = $this->_model;
				
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost('edit');
			if ($formData) {
				$user_id = (int)$this->_request->getPost('user_id');
				$row     = $edit->fetchRow('user_id='.$user_id);
				if(!$this->_request->getPost('phone')) { $phone = NULL; } else { $phone = $this->_request->getPost('phone'); }
				$row->name          = $this->_request->getPost('name');
				$row->email         = $this->_request->getPost('email');
				$row->passwd        = $this->_request->getPost('passwd');
				$row->category      = $this->_request->getPost('category');
				$row->phone         = $phone;
				// $row->group_id      = $this->_request->getPost('group_id');

				$row->save();
				
				return $this->_helper->redirector('list');
			}
		}
		$this->view->edit_user = $edit->fetchRow('user_id='.$user_id);
		/*
		require_once APPLICATION_PATH . '/models/HmGroups.php';
		$table = new Model_HmGroups();
		
		$view_list = $table->fetchAll();
		$this->view->view_list = $view_list;
		*/
	}

	public function deleteAction()
	{
		$this->initSession();
		$user_id = (int)$this->_request->getParam('user_id');
		if (($user_id !== $this->id) && ($user_id > 1)) {
			$model = $this->_getModel('HmUser');
			$remove = $this->_model;
			
			$where = 'user_id = ' . $user_id;
			$remove->delete($where);
		}
		return $this->_helper->redirector('list');
	}
/*
	protected function _getUgModel()
    {
        if (null === $this->_model) {
            require_once APPLICATION_PATH . 'models/HmGroups.php';
            $this->_model = new Model_groupsForSelect();
        }
        return $this->_model;
    }
*/
}